const express = require('express');
const bcrypt = require('bcryptjs')
const response = require('../../utils/response');
const Task = require('../../models/task');
const journal = require('../../models/journal');
const Notice = require('../../models/notice');
const router = express.Router();

//获取日志列表
router.get('/getJournal', async (req, res) => {
    const pagenum = Number(req.query.pagenum)
    const pagesize = Number(req.query.pagesize)
    var query = String(req.query.query)
    if (query == 'undefined') {
        var query = ''
    }
    const data = await journal.find({
            taskname: {
                $regex: query
            },
            operate: {
                $lt: 6
            }
        })
        .skip(pagesize * (pagenum - 1))
        .limit(pagesize)
        .populate('userId', ['name'])
        .populate('worker', ['name'])
        .sort({
            createdAt: -1
        })
        .lean()
    const total = await journal.find({
        operate: {
            $lt: 6
        }
    }).countDocuments()
    const taskCount = await Task.find().count()
    const taskCompleteCount = await Task.find({
        status: 3,
    }).count()


    response(res, 200, '日志列表成功', {
        data,
        total,
        pagenum,
        taskCount,
        taskCompleteCount
    })
})

//获取journal任务动态
// router.get('/taskDynamics/:taskId', async (req, res) => {
//     const taskId = req.params.taskId
//     const pagenum = Number(req.query.pagenum)
//     const pagesize = Number(req.query.pagesize)
//     const data = await journal.find({
//             taskId: taskId,
//             operate: {
//                 $lt: 6
//             }
//         })
//         .skip(pagesize * (pagenum - 1))
//         .limit(pagesize)
//         .populate('userId', ['name'])
//         .populate('worker', ['name'])
//         .sort({
//             createdAt: -1
//         })
//         .lean()
//     const total = await journal.find({
//         taskId: taskId,
//         operate: {
//             $lt: 6
//         }
//     }).countDocuments()

//     response(res, 200, '日志列表成功', {
//         data,
//         total,
//         pagenum,
//     })
// })


//删除日志记录
router.get('/delJournal/:journalId', async (req, res) => {
    const journalId = req.params.journalId
    const find = await journal.findById(journalId)
    if (!find) {
        response(res, 404, '找不到该任务', {
            data
        })
        return
    }
    const data = await journal.findByIdAndDelete(journalId)
    response(res, 200, '删除日志成功', {
        data
    })
})


//获取消息列表
router.get('/taskDynamics', async (req, res) => {
    const pagenum = Number(req.query.pagenum)
    const pagesize = Number(req.query.pagesize)
    var query = String(req.query.query)
    if (query == 'undefined') {
        var query = ''
    }
    const data = await Notice.find({
            tips: {
                $regex: query
            },
        })
        .skip(pagesize * (pagenum - 1))
        .limit(pagesize)
        .populate('userId', ['name'])
        .populate('notifier', ['name'])
        .populate('taskId', ['taskname'])
        .sort({
            createdAt: -1
        })
        .lean()
    const total = await Notice.find().countDocuments()

    response(res, 200, '日志列表成功', {
        data,
        total,
        pagenum,
    })
})



//删除消息
router.get('/delNotice/:noticeId', async (req, res) => {
    const noticeId = req.params.noticeId
    const find = await Notice.findById(noticeId)
    if (!find) {
        response(res, 404, '找不到该任务', {
            data
        })
        return
    }
    const data = await Notice.findByIdAndDelete(noticeId)
    response(res, 200, '删除记录成功', {
        data
    })
})



//获取单个消息任务动态
router.get('/taskDynamics/:taskId', async (req, res) => {
    const taskId = req.params.taskId
    const pagenum = Number(req.query.pagenum)
    const pagesize = Number(req.query.pagesize)
    const data = await Notice.find({
            taskId: taskId,
        })
        .skip(pagesize * (pagenum - 1))
        .limit(pagesize)
        .populate('userId', ['name'])
        .populate('notifier', ['name'])
        .sort({
            createdAt: -1
        })
        .lean()
    const total = await Notice.find({
        taskId: taskId,
    }).countDocuments()

    response(res, 200, '日志列表成功', {
        data,
        total,
        pagenum,
    })
})


module.exports = router;